﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HA.ModFlow.Domain.FileLayer;

namespace HA.ModFlow.Domain.DomainLayer
{
    [Serializable]
   public  class RiverCollection:BoundaryCollecionBase
    {
        public RiverCollection(int nrows, int ncolumns, int nlayers)
            : base(nrows, ncolumns, nlayers)
        {
            this._innerList = new List<River>();

        }


        List<River> _innerList;

        public List<River> InnerList
        {
            get { return _innerList; }
           
        }


   



        #region 公共函数


        public void Add(River river)
        {

            this._innerList.Add(river);


            this.AskCollectionChanged(new SCColleconChangedEventArgs(river,SCCollectionChangedType.ADD,SCType.RIVER));
 
        }



    

        public List<River> GetRiver(int index)
        {
            List<River> rivers = new List<River>();

            foreach (River item in this._innerList)
            {
                if (item.Index==index)
                {
                    rivers.Add(item);
                    
                }
                
            }

            return rivers;
        }

        /// <summary>
        /// 更新这里面的河流值
        /// </summary>
        /// <param name="rivers"></param>
        public void UpdateRivers(IList<River> rivers,List<int> indices)
        {


            //取得所有的index元素

            List<River> orgHeads = new List<River>();

            foreach (int index in indices)
            {
                orgHeads.AddRange(this.GetRiver(index));

            }


            foreach (River head in orgHeads)
            {
                this._innerList.Remove(head);


            }


            this._innerList.AddRange(rivers);


            this.AskCollectionChanged(new SCColleconChangedEventArgs(this._innerList, SCCollectionChangedType.RESET, SCType.RIVER));


            //List<River> cRivers = new List<River>();
            //cRivers.AddRange(rivers);

            //List<River> allRiver = new List<River>();
            //allRiver.AddRange(this._innerList);

            //int i=0;

            //foreach (River item in rivers)
            //{
            //    foreach (River river in allRiver)
            //    {
            //        if (river==item)
            //        {
            //            this._innerList[i] = item;
            //            cRivers.Remove(item);

            //            continue;
                        
            //        }
                    
            //    }



            //    i++;
                
            //}


            ////将没有 的加入集合中

            //this._innerList.AddRange(cRivers);


          

 
        }

        public void ResetRivers(List<River> rivers)
        {
            this._innerList.Clear();

            this._innerList.AddRange(rivers);

            this.AskCollectionChanged(new SCColleconChangedEventArgs(this._innerList, SCCollectionChangedType.RESET, SCType.RIVER));

 
        }




        List<River> _tempRivers=new List<River> ();

        bool _haveTempData = false;

        public bool HaveTempData
        {
            get { return _haveTempData; }
            
        }



        public void AddTempRivers(List<River> rivers)
        {
            this._tempRivers.AddRange(rivers);

            this._haveTempData = true;
        }


        /// <summary>
        /// 将暂存的数据提供给核心，并清空对应的选项
        /// </summary>
        public void CommitTempRivers()
        {

            if (this._haveTempData)
            {

                this.ResetRivers(this._tempRivers);

                this._tempRivers.Clear();
                
            }

           

            this._haveTempData = false;

 
        }








        public PRiver ToRiverPack()
        {
            PRiver p = new PRiver();

            p.MXACT = this._innerList.Count;

            //p.IRIVCB = 0;

            for (int i = 0; i < this.NPeriods; i++)
			{
                SPRiver sp=new SPRiver ();


                if (i == 0)
                {
                    sp.ITMP = this._innerList.Count;

                    foreach (River item in this._innerList)
                    {
                        GPointValue pv = new GPointValue(item.K, item.I, item.J, item.Stage[i], item.Conductance[i], item.Elevation[i]);


                        sp.RiverData.Add(pv);
                    }


                }

                else
                {
                    if (this.IsUsePrecious[i] == true)
                    {
                        sp.ITMP = -1;

                    }
                    else
                    {

                        sp.ITMP = this._innerList.Count;

                        foreach (River item in this._innerList)
                        {
                            GPointValue pv = new GPointValue(item.K, item.I, item.J, item.Stage[i], item.Conductance[i], item.Elevation[i]);


                            sp.RiverData.Add(pv);
                        }
 
                    }
                }

               


              p.StressPeriodRiver.Add(sp);


                
			 
			}

           

            return p;
        }


        public override void _stressPeriods_StressPeriodChanged(object sender, StressPeriodChangedEventArgs e)
        {

            //基类的函数必须调用

            base._stressPeriods_StressPeriodChanged(sender, e);






            List<StressPeriod> lst = (List<StressPeriod>)e.Param;


         

            foreach (River item in this._innerList)
            {
                item.OnResetSperiods(lst.Count);
                
            }

            
        }


        public void Clear()
        {
            this._innerList.Clear();

            this.AskCollectionChanged(new SCColleconChangedEventArgs(this._innerList, SCCollectionChangedType.RESET, SCType.RIVER));


            
        }

        public override void OnGridReset(int nrows, int ncols, int nlayers)
        {
            base.OnGridReset(nrows, ncols, nlayers);

            this.Clear();

        }

        #endregion

    }
}
